<?php

// +----------------------------------------------------------------------
// | EasyAdmin
// +----------------------------------------------------------------------
// | PHP交流群: 763822524
// +----------------------------------------------------------------------
// | 开源协议  https://mit-license.org 
// +----------------------------------------------------------------------
// | github开源项目：https://github.com/zhongshaofa/EasyAdmin
// +----------------------------------------------------------------------

namespace app\admin\controller\management;


use app\admin\model\management\Store as StoreModel;
use app\admin\model\management\Area as AreaModel;
use app\admin\service\TriggerService;
use app\common\constants\AdminConstant;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use think\App;
use think\facade\Request;
use think\facade\Db;
use app\admin\model\ManagementStoreBecome;
/**
 * Class Admin
 * @package app\admin\controller\system
 * @ControllerAnnotation(title="管理员管理")
 */
class Store extends AdminController
{

    use \app\admin\traits\Curd;
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new StoreModel();
        $this->area  =new AreaModel();
    }
    public function getarea(){
        $area  =$this->area->getArea();
        $data  = [
            'code'  => 0,
            'msg'   => '',
            'data'  => $area,
        ];
        return json($data);
    }
    /**
     * @NodeAnotation(title="列表")
     */
    public function index()
    {
        if ($this->request->isAjax()) {
            if (input('selectFields')) {
                return $this->selectList();
            }
            list($page, $limit, $where) = $this->buildTableParames();
            $count = $this->model->where($where)->count();
            $list  = $this->model->order('id asc')->where($where)->page($page, $limit)->select();
       
            $data  = [
                'code'  => 0,
                'msg'   => '',
                'count' => $count,
                'data'  => $list,
            ];
            return json($data);
        }
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="添加")
     */
    public function add($id = null)
    {
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $rule = [
                // 'cate_id|cate_id'    => 'require|>:0',
            ];
            $this->validate($post, $rule);
            $area=$this->area->where('id',$post['address_ids'])->field('merger_name,pid')->find();
            $post['store_address']=str_replace('中国','',str_replace(',','',$area['merger_name']));
            $post['area_id']=$area['pid'];

            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败');
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        $this->assign('id', $id);
        return $this->fetch();
    }

    /**
     * @NodeAnotation(title="编辑")
     */
    public function edit($id)
    {   
    
        $row = $this->model->where('id',$id)->find();
        empty($row) && $this->error('数据不存在');
        if ($this->request->isAjax()) {
            $post = $this->request->post();
            $area=$this->area->where('id',$post['address_ids'])->field('merger_name,pid')->find();
            $post['address']=str_replace('中国','',str_replace(',','',$area['merger_name']));
            $post['area_id']=$area['pid'];
            $rule = [];
            $this->validate($post, $rule);
          
            try {
                $save = $row->save($post);
               
            } catch (\Exception $e) {
                $this->error($e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
      
        $this->assign('row', $row);
        return $this->fetch();
    }
    /**
     * @NodeAnotation(title="删除")
     */
    public function delete($id)
    {  
    
        $row = $this->model->whereIn('id', $id)->select();
     
        $row->isEmpty() && $this->error('数据不存在');
        try {
            $save = $this->model->whereIn('id', $id)->delete();
       
            foreach ($row as $k => $v) {
                $store_image=explode('|',$v['image']);
                foreach ($store_image as $k1 => $v1) {
                    del_image($v1);
                }
            }
            
        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $save ? $this->success('删除成功') : $this->error('删除失败');
    }

 /**
     * 
     * @NodeAnotation(title="属性修改")
     */
    public function modify()
    {
        $post = $this->request->post();
     
        $rule = [
            // 'cate_id|ID'    => 'require',
            'field|字段' => 'require',
            'value|值'  => 'require',
        ];
        $this->validate($post, $rule);
        // if (!in_array($post['field'], $this->allowModifyFields)) {
        //     $this->error('该字段不允许修改：' . $post['field']);
        // }
     
        $row = $this->model->where('id',$post['id'])->find();
        empty($row) && $this->error('数据不存在');
        try {
            $row->save([
                $post['field'] => $post['value'],
            ]);
        } catch (\Exception $e) {
            // $this->error($e->getMessage());
            $this->success('保存成功');
        }
        $this->success('保存成功');
    }
   
  //所有店铺
    public function getstore(){
        
        $post=input('get.');
        $where=[];
        if(isset($post['area_id'])){
            $where['area_id']=$post['area_id'];
        }
        $list  = $this->model->field('id as store_id,store_name')->where($where)->order('id asc')->select();
        $data  = [
            'code'  => 1,
            'msg'   => '',
            'data'  => $list,
        ];
       
        return json($data);
    }
    
}
